local skel = fk.CreateSkill{
  name = "tea__chuangxun",
}
-- local U = require "packages/utility/utility"
Fk:loadTranslationTable{
  ["tea__chuangxun"] = "创勋",
  [":tea__chuangxun"] = "当你受到过伤害的回合结束时，你可以分配牌堆顶〔2〕张牌，且本回合每有一项被执行过，你令〔〕内的数字翻倍：1.你造成过伤害；2.有角色进入过濒死状态。",
  ["#tea__chuangxun-invoke"] = "创勋：你可以分配排队顶的 %arg 张牌！",
  ["#tea__chuangxun-distribute"] = "创勋：分配这些牌，未分配的牌默认留给自己",
}
---@type TrigSkelSpec<TurnFunc>
local chuangxun = {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) then
      local turn_id = player.room.logic:getCurrentEvent().id
      local events = player.room.logic:getEventsByRule(GameEvent.Damage, 1, function (e)
        return e.data.to and e.data.to == player
      end, turn_id)
      return #events > 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local num = 2
    local turn_id = player.room.logic:getCurrentEvent().id
    local cause_damage_events = player.room.logic:getEventsByRule(GameEvent.Damage, 1, function (e)
      return e.data.from and e.data.from == player
    end, turn_id)
    local dying_events = player.room.logic:getEventsByRule(GameEvent.Dying, 1, Util.TrueFunc, turn_id)
    if #cause_damage_events > 0 then num = num * 2 end
    if #dying_events > 0 then num = num * 2 end
    if player.room:askToSkillInvoke(player, {
      skill_name = skel.name,
      prompt = "#tea__chuangxun-invoke:::" .. num
    }) then
      event:setCostData(self, { num = num })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local num = event:getCostData(self).num ---@type integer
    local vioce_num = { [2] = 1, [4] = 2, [8] = 3 }
    player:broadcastSkillInvoke(skel.name, vioce_num[num])
    room:notifySkillInvoked(player, skel.name, "special")
    local cards = room:getNCards(num)
    local dict = room:askToYiji(player, {
      cards = cards,
      expand_pile = cards,
      targets = room:getAlivePlayers(),
      min_num = 1,
      max_num = 999,
      skill_name = skel.name,
      prompt = "#tea__chuangxun-distribute",
      skip = true,
    })
    local dis_cards = {}
    for _, cids in pairs(dict) do
      table.insertTable(dis_cards, cids)
    end
    local remian_cards = table.filter(cards, function(id)
      return not table.contains(dis_cards, id)
    end)
    if #remian_cards > 0 then
      local player_cards = table.simpleClone(dict[player.id] or {})
      table.insertTable(player_cards, remian_cards)
      dict[player.id] = player_cards
    end
    room:doYiji(dict)
  end
}
skel:addEffect(fk.TurnEnd, chuangxun)
-- Fk.generals["mouxusheng"]:addSkill(skel.name)
return skel